


// variables which can be configured on client
if(typeof namelink_action == 'undefined') {
    var namelink_action = "replace_with_link";
}
if(typeof namelink_sources == 'undefined') {
    var namelink_sources = ['gni'];
}
if(typeof namelink_callback == 'undefined') {
    var namelink_callback = 'callback_namelink_replace_tag';
}
if(typeof namelink_show_logo == 'undefined') {
    var namelink_show_logo = false;
}
if(typeof namelink_show_common_name == 'undefined') {
    var namelink_show_common_name = false;
}
if(typeof namelink_show_menu == 'undefined') {
    var namelink_show_menu = false;
}
if(typeof number_of_asynchronous_calls) {
    var number_of_asynchronous_calls = 5;
}

var namelink_api_lookup = 'gni_providers';

if(namelink_show_common_name == true && namelink_sources[0] == 'eol')
{
    namelink_api_lookup = 'eol';
}

for(var i=0 ; i<namelink_sources.length ; i++)
{
    namelink_sources[i] = namelink_sources[i].toLowerCase();
    if(namelink_sources[i] == "col") {
        namelink_sources[i] = "catalogue of life";
    }
}







// starts replacing the tagged names
function namelink_replace_tags()
{
    var number_of_tags = $('namelink').size();
    var start_index = index_of_next_tag;
    var end_index = start_index + number_of_asynchronous_calls;
    waiting_on_index = null;
    
    if(end_index > number_of_tags) {
        end_index = number_of_tags;
    }
    
    // no more tags left - stop the process
    if(start_index >= end_index-1) {
        return;
    }
    
    for(var i=start_index ; i<end_index ; i++)
    {
        var this_tag = $("namelink").get(i);
        var predicted = $(this_tag).attr('predicted');
        var resume = 0;
        
        // this is the last element in this batch
        // set this variable so process doesn't continue until this tag has been replaced
        if(i == end_index-1) {
            waiting_on_index = i;
        }
        
        index_of_next_tag = i+1;
        include_js(name_search_api_prefix + '?collection='+ namelink_api_lookup +'&name_string='+ escape(predicted) +'&index='+ i +'&callback='+namelink_callback);
    }
}


//=========================

function callback_namelink_replace_tag(obj, index)
{
    var element = $($("namelink").get(index));

    switch(namelink_action)
    {
        case 'replace_with_link':
            replace_with_link(obj, element);
            break;
        default:
            break;
    }
    
    if(index == waiting_on_index) {
        
        // this resumes processing of the tags
        // the process needs to be broken up so not all queries are made simultaneously
        namelink_replace_tags();
    }
}

function replace_with_link(obj, element)
{
    var providers_array = [];
    
    for (var i = 0; i < obj.length; i++)
    {
        var source_title = obj[i].source_title;
        providers_array[source_title.toLowerCase()] = obj[i];
    }
    
    for (var i=0 ; i < namelink_sources.length ; i++)
    {
        var collection = namelink_sources[i];
        if(providers_array[collection])
        {
            var obj = providers_array[collection];
            var name_string = obj.name_string;
            var url = obj.url;
            var logo_url = obj.logo_url;
            var common_name = obj.common_name;
            
            
            element.wrapInner('<a href="' + url + '" target="_blank"><u>');
            if(namelink_show_common_name && common_name) {
                element.append('&nbsp;(<a href="' + url + '" target="_blank"><strong>' + common_name + '</strong></a>)');
            }
            if(namelink_show_logo && logo_url) {
                element.after('&nbsp;<a href="' + url + '" target="_blank"><img src="' + logo_url + '" width="20" border="0"/></a>');
            }
            break;
        }
    }
}






// dynamically include external javascript
function include_js(location)
{
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = location;
    
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(script);
}